//活动详情页面
Cn_ActView ={
	DATA : {}
	,isTimeOff :''//活动状态
	//活动详情页面—初始化
	,viewInit : function(){
		Jes.userSess();
		Jes.setToday('today');
		Jes('users').value = '';//选中数据的清空
		var actid = Jes.request('id');
		if(!actid) return;
		Jes('actid').value = actid;
		Jes.query('cnactview_query_sql@act',{paraObj:"{id:'"+actid+"'}",'isSess':0,isLoad:true},function(data){
			data= data[0];
			Cn_ActView._actViewHtml(data);
			Cn_ActView.actMan(actid);
			Cn_ActView.actPlun(actid);
			Jes.locate(data.address,'map_canvas');
		});
		Cn_ActView.actpicshow(actid);
	}
	,actpicshow : function(actid){
		Jes.page('actpic_query_sql@idx',{paraObj:"{actid:'"+actid+"'}",'isSess':0,page:1,pageSize:12,isLoad:false},function(data){
			var total=data.totalNum,data = data.pageList,html='';
			for(var i=0;i<data.length;i++){
				html += '<div>'+
	        		'<div class="pic"><a href="viewpic.html?picid='+data[i].id+'" >'+
	        		'<img src="'+CONFIG_DATA.actpath+data[i].spic+'" />'+
	        		'</a>'+
	        		'</div>'+
		        	'<br/>'+
		        	'<span class="pl">来自 </span>';
		        	if(data[i].type=='2'){
		        		html += '<a href="../front/indexbus.html?uid='+data[i].userid+'">'+data[i].username+'</a>';
		        	}else{
		        		html += '<a href="../front/person.html?uid='+data[i].userid+'">'+data[i].username+'</a>';
		        	}
	        		html += '</div>';
	        	if(((i+1)/4).toString().indexOf('.')==-1){
					html += '<div class="clear"></div>';
				}
			}
			if(total>0){
				html = '<h2>活动照片 ···<span class="pl">( <a href="scanpic.html?id='+actid+'">全部'+total+'张</a> )</span></h2>'+html;
			}
			Jes.html('showactpic',html);
		})
	}
	,_actViewHtml : function(data){
			if(userInfo!=null)Cn_ActView.DATA['isCU'] = userInfo.userid == data.userid;//判断当前用户是否创建人
			Jes('createrid').value = data.userid;//活动创建人id
			if(Cn_ActView.DATA['isCU']){//发起人的权限功能
				Jes.show('seluser');//邀请好友
				Jes.show('deleteact');//删除活动
			}
			Jes.html('content',data.content);
			Jes.html('timestar',Jes.getDateStrMoonDay(data.acttime) +' '+this.getTimeWeekStr(data.acttime)+ ' '+data.acthour + ':00');
			Jes.html('timeend',Jes.getDateStrMoonDay(data.endtime) +' '+this.getTimeWeekStr(data.endtime)+' '+data.endhour + ':00');
			if(data.busname) Jes.html('_busname','  <a href="../bus/busview.html?id='+data.busid+'" target="blank">'+data.busname+'</a>');
			if(data.creatertype=='0'){//官方活动
				Jes.html('hdleftcon','<img src="../themes/default/images/sd03.png" width="90" height="90" class="sd_gftj"/><div class="pic">'
					+ '<img id="hdpic" src="'+CONFIG_DATA.path+data.actpic+'" width="365" height="258"/></div>');
				Jes.show('hidepl');
			}else{
				Jes.html('hdleftcon','<div class="pic"><img id="hdpic" src="'+CONFIG_DATA.path+data.actpic+'" width="365" height="258"/></div>'
				+'<a class="hdxq " id="hdxq" href="javascript:void(0);" onclick=\'showhidediv("hidepl");\'>活动详情</a>');
			}
			Jes.html('factnum',data.factnum+'人参加');
			var feetype='';
			if(data.feetype=='0'){feetype='免费'}
			if(data.feetype=='1'){feetype='发起人请客'}
			if(data.feetype=='2'){feetype='AA制'}
			var intro = '';
			if(data.money==null||data.money==''||data.money=='0'){//不花钱的活动
				Jes.html('money','0');
				Cn_ActView.DATA['isFree']=true;
			}else{
				Cn_ActView.DATA['isFree']=false;
				Jes.html('money',data.money);
				intro = '，人均'+data.capita+'(仅参考),';
			}
			var lastnum = (data.maxnum || 100)-data.factnum , hdxz='';
			if(data.actpro==1){ hdxz=  '公开邀请(所有人可以参加)';}else{hdxz= '私人邀请(此活动仅被邀请者才能看到)';}
			Jes.html('mun',lastnum);
			Jes.html('hdmap','活动地点('+ data.address +')');
			var viewHtml = '【活动主题】：'+ data.content 
				+'<br/>【活动时间】：开始时间:'+ data.acttime + ' '+ data.acthour 
				+':00    结束时间:'+data.endtime+' '+data.endhour+':00  <br/><span >【活动地点】：</span>'+ data.address 
				+'<br/>【邀请性质】：'+ hdxz
				+'<br/>【参加人数】：  '+ data.factnum + '/' +  data.maxnum +'(人)'
				+'<br/>【活动简介】：'+ data.remark +'<br/>';
			Jes.html('hidepl',viewHtml);
			Jes('topicid').value = data.topicid;//活动主题
			if(data.maxnum==data.factnum){ Cn_ActView.DATA['surplus']=1;}else{Cn_ActView.DATA['surplus']=0;}//人数已满
			var ii = data.endtime+' '+data.endhour+':00:00';
			var oo = new Date(ii.replace(/-/g,"/"));
			if(oo<= new Date())Cn_ActView.isTimeOff=1;//活动已过期
	}
	//活动成员 actid 活动id  type tab页
	,actMan : function(actid){
		Jes.query('cnactjoinuser_query_sql@act',{paraObj:"{actid:'"+actid+"'}",'isSess':0,isLoad:false},function(data){
			var join_user =[],ask_user=[],invite_user=[],jnum=0,anum=0,inum=0,imgsrc,loginid,userid,isCU=false;
			Jes('alluserids').value='';
			Jes.each(data,function(i){
				imgsrc=Jes.getPicPath(data[i].pic),loginid=data[i].loginid;userid=data[i].userid;
				if(userInfo!=null){if(userInfo.userid == data[i].userid) Cn_ActView.status = data[i].status;}
				if(data[i].status=='2'){
					jnum++;
					join_user.push('<li onmouseover="Comm.namecardshow(\''+userid+'\',\'act_'+ userid + data[i].status+'\')" onmouseout="Comm.namecardhide(\'act_'+ userid + data[i].status+'\')">'
                    	+'<a href="javascript:void(0);" ><img src= "'+imgsrc+'"/></a><p>'+loginid.substr(0,8)+'</p>'
                    	+'<div id="act_'+ userid +data[i].status+'" ></div></li>');
				}else if(data[i].status=='1'){
					if(Cn_ActView.DATA['isCU']){
						anum++;
						ask_user.push('<li id="askli'+userid+'"  onmouseover="Comm.namecardshow(\''+userid+'\',\'act_'+ userid + data[i].status+'\')" onmouseout="Comm.namecardhide(\'act_'+ userid + data[i].status+'\')">'
                    	+'<a onclick="Cn_ActView.seleted(\''+userid+'\')" class="choose"><img src= "'+imgsrc+'" /><span  class="chopic"></span></a>'
                    	+'<div id="act_'+ userid +data[i].status+'" ></div></li>');
                    	Jes('alluserids').value = Jes('alluserids').value + userid + ',';
					}else{
						anum++;
						ask_user.push('<li id="askli'+userid+'"  onmouseover="Comm.namecardshow(\''+userid+'\',\'act_'+ userid + data[i].status+'\')" onmouseout="Comm.namecardhide(\'act_'+ userid + data[i].status+'\')">'
                    	+'<a href="javascript:void(0);" ><img src= "'+imgsrc+'" /></a><p>'+loginid.substr(0,8)+'</p>'
                    	+'<div id="act_'+ userid +data[i].status+'" ></div></li>');
					}
				}else if(data[i].status=='0'){
					inum++;
					invite_user.push('<li onmouseover="Comm.namecardshow(\''+userid+'\',\'act_'+ userid + data[i].status+'\')" onmouseout="Comm.namecardhide(\'act_'+ userid + data[i].status+'\')">'
                    	+'<a href="javascript:void(0);" ><img src= "'+imgsrc+'"/><span></span></a>'
                    	+'<div id="act_'+ userid +data[i].status+'" ></div></li>');
				}
			});
			if(Cn_ActView.status=='2')Jes.show('addactpic');
			Cn_ActView.showOp(Cn_ActView.status);//根据用户状态显示不同按钮
			Jes.html('join_user',join_user),Jes.html('ask_user',ask_user),Jes.html('invite_user',invite_user);
			Jes.html('jnum','('+jnum+'人)'),Jes.html('anum','('+anum+'人)'),Jes.html('inum','('+inum+'人)');
		});
	}
	//活动详情页面___初始化评论部分
	,actPlun : function(actid){
		Jes.query('actpl_query_sql@act',{paraObj:"{actid:'"+actid+"'}",'isSess':0,isLoad:false},function(data){
			var html=[];
			for(var i=0;i<data.length;i++){
				html.push(Comm._actPlunHtml(data[i],i));
			}
			Jes.html('commentscontent',html);
		});
	}
	//活动详情页面___评论回复初始化
	,reply : function(loginid,replyid){
		Jes('plContent').value = '回复@'+loginid+':';
		var txt = Jes('plContent').createTextRange();//光标定位
		txt.moveStart('character',Jes('plContent').value.length);
		txt.collapse(true);
		txt.select();
		Jes('replyid').value=replyid;
	}
	//活动详情页面___发评论
	,doPlun: function(){
		var id = Jes('topicid').value,plVal=Jes('plContent').value,plzf = Jes('plzf'),rid=Jes('replyid').value;
		var actid = Jes.request('id');
		if(plVal==''){
			Jes.alert('评论内容不能为空!');
			pl.focus();
			return;
		}
		Jes.doSaveTObject('CnComment',{param:{'topicid':id,content:plVal,reuserid:rid,iszf:plzf.checked?'1':'0'}},function(data){
			Jes('plContent').value='';
			plzf.checked=false;
			var actid = Jes.request('id');
			Cn_ActView.actPlun(actid);
		});
	}
	//评论的删除
	,delActPlun : function(obj,commentId,topicid){
		if(!confirm("你确定删除评论吗？")) return;
		Jes.executeSql({ids:commentId,topicid:topicid,sql:'cncomment_delete_hql'},function(){
			var el = obj.parentNode.parentNode;
			el.parentNode.removeChild(el);
		});
	}
	//全选的事件
	,allSeleted : function(obj){
		var allusers = Jes('alluserids').value.split(',');
		if(obj.checked){
			for(var i=0;i<allusers.length-1;i++){
				if($('#askli'+allusers[i]).find('a').hasClass('onchoose')){
					null;
				}else{
					Jes('users').value=Jes('users').value + allusers[i] + ',';
					$('#askli'+allusers[i]).find('a').addClass('onchoose');
				}
			}
		}else{
			for(var i=0;i<allusers.length-1;i++){
			if($('#askli'+allusers[i]).find('a').hasClass('onchoose')){
				$('#askli'+allusers[i]).find('a').removeClass('onchoose');
				Jes('users').value=Jes('users').value.replace(allusers[i]+',','');
			}else{
				null;
			}
			}
		}
	}
	//选择申请人
	,seleted : function(div){
		if($('#askli'+div).find('a').hasClass('onchoose')){
			$('#askli'+div).find('a').removeClass('onchoose');
			Jes('users').value=Jes('users').value.replace(div+',','');
		}else{
			Jes('users').value=Jes('users').value+div + ',';
			$('#askli'+div).find('a').addClass('onchoose');
		}
	}
	//显示tab
	,showTab : function(tab){
		for(var i=1;i<4;i++){
			if(i!=tab) {
				$('#tab1'+i).removeClass('tabseled');
				Jes('tdivs1'+i).style.display ='none';
			}
		}
		if(tab=='2'){//审核按钮
			Jes('titlerb').style.display ='block';
		}else{
			Jes('titlerb').style.display ='none';
			document.getElementById('titlerb').style.display ='none';
		}
		$('#tab1'+tab).addClass('tabseled');
		Jes('tdivs1'+tab).style.display ='block';
	}
	//目前状态
	,showOp : function(status){
		if(Cn_ActView.isTimeOff==1){//活动已结束
			Jes.html('status','<span class="joined" style="display:block!important;">成功结束</span>');
		}else{
			switch (status) {
				case "0" ://被邀请
					Jes.html('status','<a href="javascript:void(0);" onclick="Cn_ActView.doJoinAct();"><span class="want" style="display:block!important;"  >接受邀请</span></a>');
					break;
				case "1" ://已申请
					Jes.html('status','<span class="wait" style="display:block!important;">等待通过</span>');
					break;
				case "2" ://已参加  有邀请人的授权
					Jes.show('seluser');
					Jes.html('status','<span class="joined" style="display:block!important;">您已参加</span>');
					break;
				case "3": //被拒绝？
					break;
				default :
					if(Cn_ActView.DATA['surplus']==1){//人数已满
						Jes.html('status','<span class="joined" style="display:block!important;">人数已满</span>');
					}else{
						Jes.html('status','<a href="javascript:void(0);" onclick="Cn_ActView.doJoinAct();"><span class="want" style="display:block!important;"  >我要参加</span></a>');
					}
				    break;
			}
		}
	}
	//参加活动 按钮
	,doJoinAct : function(isag,userid){
	//	if(!Jes.userSess(1))return;
		if(Cn_ActView.DATA['isFree']){
			Cn_ActView.joinAct(isag,userid);
		}else{
			var actid = Jes.request('id');
			window.location.href='actdeal.html?id='+actid;
		}
	}
	//直接加入
	,joinAct : function(isag,userid){
		if(!Jes.userSess(1))return;
		var actid = Jes.request('id');
		Jes.save('cn.joinact',{param:{'actid':actid,'isag':isag,'createrid':userid}},function(data){
			//Jes.alert(data+',你将被授于邀请其他好友的权利');
			location.reload();
		});	
	}
	//取时间格式 return 周四
	,getTimeWeekStr : function (Time) {
    	var dd= new Date(Date.parse(Time.replace(/-/g, "/")));
    	var ww = dd.getDay();
    	if (ww==0) ww="周日";
		if (ww==1) ww="周一";
		if (ww==2) ww="周二";
		if (ww==3) ww="周三";
		if (ww==4) ww="周四";
		if (ww==5) ww="周五";
		if (ww==6) ww="周六";
    	return ww;
	}
}